<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/31 11:42
 */

namespace App\Admin\Controllers;
use App\Http\Controllers\Controller;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\CustomerService;

use Auth;
use DB;
use Illuminate\Http\Request;
use function Sodium\crypto_box_publickey_from_secretkey;
use Illuminate\Support\MessageBag;

class CustomerController  extends Controller
{
    use ModelForm;
    protected $headerStr = '日志';
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr);
            $content->description('客服列表');
            $content->body($this->grid());
        });

    }
    protected function form()
    {
        return Admin::form(CustomerService::class, function (Form $form) {

            $form->display('id', 'ID:');
            $form->text('qq', 'QQ:');
            $form->text('nickName', '昵称:');
            $form->select('type', '客服类型:')->options([ '1'=>'运营客服','2'=>'商务合作客服', '3'=>'技术客服']);
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                if(method_exists('tools','disableView')){
                    $tools->disableView();
                }
            });
        });
    }

    protected function grid()
    {
        return Admin::grid(CustomerService::class, function (Grid $grid) {
            $grid->id('编号')->sortable();
            $grid->qq('QQ');
            $grid->nickName('昵称');
            $grid->type('类型')->display(function($type){
                //1运营客服,2商务合作客服3技术客服
                return $type == 2 ? '<span class=" label label-success">商务合作客服</span>' : ($type == 3 ? '<span class=" label label-warning">技术客服</span>'  : '<span class=" label label-default">运营客服</span>');
            });;
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->equal('type', '客服类型')->select([ '1'=>'运营客服','2'=>'商务合作客服', '3'=>'技术客服']);
            });
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->actions(function ($actions) {
                //$actions->disableDelete();
                //$actions->disableView();
                if(method_exists($actions,'disableView')){
                    $actions->disableView();
                }
            });
            $grid->paginate(env('Admin_Limit'));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr.'管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->headerStr.'管理');
            $content->body($this->form());
        });
    }
    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

}